package com.beagle.njddapp.ui.presenter;

import com.beagle.njddapp.base.ApiService;
import com.beagle.njddapp.base.BasePresenter;
import com.beagle.njddapp.base.DirverApiService;
import com.beagle.njddapp.base.NjOwnerApiService;
import com.beagle.njddapp.base.RetrofitHttp;
import com.beagle.njddapp.bean.owner.DIrverOrderBean;
import com.beagle.njddapp.core.LocationManager;
import com.beagle.njddapp.net.BaseOilHttpListResponse;
import com.beagle.njddapp.net.BaseOilHttpResponse;
import com.beagle.njddapp.net.BaseStringResponseBean;
import com.beagle.njddapp.net.HttpResponseObserver;
import com.beagle.njddapp.net.RxSchedulers;
import com.beagle.njddapp.ui.view.DirverHomeView;
import com.google.gson.Gson;

import java.util.HashMap;
import java.util.LinkedHashMap;

import okhttp3.RequestBody;

/**
 * @author hxx
 * @date 2021-03-02 22:33
 */
public class DirverHomePresenter extends BasePresenter<DirverHomeView> {
    private DirverApiService dirverApiService;
    private ApiService apiService;
    private NjOwnerApiService njOwnerApiService;


    public DirverHomePresenter() {
        retrofit2.Retrofit retrofit = RetrofitHttp.provideRetrofit();
        //获取接口对象
        dirverApiService = retrofit.create(DirverApiService.class);
        apiService = retrofit.create(ApiService.class);
        njOwnerApiService = retrofit.create(NjOwnerApiService.class);


    }

    public void getDirverOrder(int pageSize, int page) {
        Gson gson = new Gson();
        HashMap<String, String> paramsMap = new HashMap<>();
        paramsMap.put("latitude", LocationManager.getInstance().getLatitude() + "");
        paramsMap.put("longitude", LocationManager.getInstance().getLongitude() + "");
        String strEntity = gson.toJson(paramsMap);
        RequestBody body = RequestBody.create(okhttp3.MediaType.parse("application/json;charset=UTF-8"), strEntity);
        dirverApiService.queryOrderListByDriver(pageSize + "", page + "", body).compose(RxSchedulers.compose()).safeSubscribe(new HttpResponseObserver<BaseOilHttpListResponse<DIrverOrderBean>>() {
            @Override
            public void success(BaseOilHttpListResponse<DIrverOrderBean> dirverHomeBeanBaseOilHttpListResponse) {
                if (dirverHomeBeanBaseOilHttpListResponse.results != null) {
                    getView().showLIst(dirverHomeBeanBaseOilHttpListResponse.results);
                } else {
                    getView().onError("");
                }
            }

            @Override
            public void error(int code, String message) {
                getView().onError(message);
            }
        });
    }

    //    public void createRequest(String meshId,String formToken){
//        Gson gson=new Gson();
//        HashMap<String,String> paramsMap=new HashMap<>();
//        paramsMap.put("meshId", meshId);
//        paramsMap.put("formToken", formToken);
//        String strEntity = gson.toJson(paramsMap);
//        RequestBody body =RequestBody.create(okhttp3.MediaType.parse("application/json;charset=UTF-8"),strEntity);
//        dirverApiService.createRequest(body).compose(RxSchedulers.compose()).safeSubscribe(new HttpResponseObserver() {
//            @Override
//            public void success(Object o) {
//
//            }
//
//            @Override
//            public void error(int code, String message) {
//
//            }
//        });
//    }
//    public void requestFormToken(){
//        Gson gson=new Gson();
//        HashMap<String,String> paramsMap=new HashMap<>();
//        paramsMap.put("type", "fuelRequest");
//        String strEntity = gson.toJson(paramsMap);
//        RequestBody body =RequestBody.create(okhttp3.MediaType.parse("application/json;charset=UTF-8"),strEntity);
//        apiService.requestFormToken(body).compose(RxSchedulers.compose()).safeSubscribe(new HttpResponseObserver<BaseStringResponseBean>() {
//            @Override
//            public void success(BaseStringResponseBean baseStringResponseBean) {
//            }
//
//            @Override
//            public void error(int code, String message) {
//                getView().onError(message);
//            }
//        });
//    }
    public void getList() {

        Gson gson = new Gson();
        HashMap<String, String> paramsMap = new LinkedHashMap<>();
        paramsMap.put("orderId", "");
        paramsMap.put("orderNum", "");
        paramsMap.put("orderStatus", "");
        String strEntity = gson.toJson(paramsMap);
        RequestBody body = RequestBody.create(okhttp3.MediaType.parse("application/json;charset=UTF-8"), strEntity);
        paramsMap.clear();
        njOwnerApiService.queryFindDriverOrderList(1000 + "", 1 + "", body).compose(RxSchedulers.compose()).safeSubscribe(new HttpResponseObserver<BaseOilHttpListResponse<DIrverOrderBean>>() {
            @Override
            public void success(BaseOilHttpListResponse<DIrverOrderBean> orderBeanBaseOilHttpListResponse) {
                if (orderBeanBaseOilHttpListResponse != null && orderBeanBaseOilHttpListResponse.results != null) {
                    if (orderBeanBaseOilHttpListResponse.results.size() > 0) {
                        getView().showLIst(orderBeanBaseOilHttpListResponse.results);
                    }else {
                        getView().onError("");
                    }

                } else {

                } getView().onError("");
            }

            @Override
            public void error(int code, String message) {
                getView().onError(message);
            }
        });
    }

    public void updateFindDriverOrder(DIrverOrderBean dirverHomeBean) {
        Gson gson = new Gson();
        String strEntity = gson.toJson(dirverHomeBean);
        RequestBody body = RequestBody.create(okhttp3.MediaType.parse("application/json;charset=UTF-8"), strEntity);
        njOwnerApiService.updateFindDriverOrder(body).compose(RxSchedulers.compose()).safeSubscribe(new HttpResponseObserver<BaseOilHttpResponse>() {
            @Override
            public void success(BaseOilHttpResponse baseOilHttpResponse) {
                getView().updateSuccess();
            }

            @Override
            public void error(int code, String message) {
                getView().onError(message);
            }
        });
    }

}
